#!/usr/bin/env -S python3 -B

import logging
from pathlib import Path

import click
import rich.logging
import rich.traceback

from .common import CON, help_all
from .error4test.cli import cli as error4test_cli
from .runner import urunner as u


def _logging(verbose: int):
    levels = [
        logging.WARNING,
        logging.INFO,
        logging.DEBUG,
        logging.NOTSET,
    ]
    level = max(min(verbose, len(levels) - 1), 0)
    logging.basicConfig(
        level=level,
        format='%(message)s',
        datefmt='[%X]',
        handlers=[
            rich.logging.RichHandler(console=CON),
        ],
    )


@help_all()
@click.option(
    '-v',
    '--verbose',
    count=True,
    default=0,
    help='Logging level.',
)
@click.group(invoke_without_command=True)
def cli(verbose: int):
    rich.traceback.install(show_locals=True)
    _logging(verbose)


cli.add_command(error4test_cli)


def runner(root_dir: Path) -> u.URunner:
    return u.URunner(
        runner=root_dir / Path('arkcompiler/runtime_core/static_core/tests/tests-u-runner/runner.sh'),
        options=u.URunnerOptions(
            build_dir=root_dir / 'build',
            work_dir=root_dir / 'build' / 'archives',
        ),
    )


def astchecker():
    return u.AstChecker(runner(Path('/home/dima/projects/panda/current/')))
    pass


if __name__ == '__main__':
    cli()
